package aeolus.wind.spider.biz.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import aeolus.wind.spider.biz.constants.Constants;
import aeolus.wind.spider.biz.dao.BDBStore;
import aeolus.wind.spider.biz.dataobject.Vote;
import aeolus.wind.spider.biz.service.interfaces.VoteService;

import com.sleepycat.je.Transaction;
import com.sleepycat.persist.EntityJoin;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.ForwardCursor;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.SecondaryIndex;

public class VoteServiceImpl implements VoteService {

    private static EntityStore                          voteStore;
    private static PrimaryIndex<String, Vote>           voteIdIndex;
    private static SecondaryIndex<String, String, Vote> votedNameIndex;
    private static SecondaryIndex<String, String, Vote> voteNameIndex;
    private static SecondaryIndex<String, String, Vote> ownIdentityNameIndex;

    static {
        BDBStore bdbStore = new BDBStore(Constants.VOTE_BIZ_NAME);
        voteStore = bdbStore.createEntityStore(Constants.VOTE_BIZ_NAME);
        // 创建各种索引
        voteIdIndex = voteStore.getPrimaryIndex(String.class, Vote.class);
        votedNameIndex = voteStore.getSecondaryIndex(voteIdIndex, String.class, "identityNameBeVoted");
        voteNameIndex = voteStore.getSecondaryIndex(voteIdIndex, String.class, "identityNameVote");
        ownIdentityNameIndex = voteStore.getSecondaryIndex(voteIdIndex, String.class, "ownIdentityName");

    }

    @Override
    public List<Vote> listVoted(String ownerIdentityName, String identityNameBeVoted) {
        List<Vote> list = new ArrayList<Vote>();
        Vote vote = null;
        EntityJoin<String, Vote> join = new EntityJoin<String, Vote>(getPi());
        join.addCondition(getIdentityNameBeVotedSi(), identityNameBeVoted);
        join.addCondition(getOwnIdentityNameSi(), ownerIdentityName);
        ForwardCursor<Vote> ec = join.entities();
        while ((vote = ec.next()) != null)
            list.add(vote);
        ec.close();
        return list;
    }

    @Override
    public Map<String, Vote> getVoteMadeBy(String ownerIdentityName, String identityNameVote) {
        Map<String, Vote> map = new HashMap<String, Vote>();
        Vote vote = null;
        EntityJoin<String, Vote> join = new EntityJoin<String, Vote>(getPi());
        join.addCondition(getIdentityNameVoteSi(), identityNameVote);
        join.addCondition(getOwnIdentityNameSi(), ownerIdentityName);
        ForwardCursor<Vote> ec = join.entities();
        while ((vote = ec.next()) != null)
            map.put(vote.getIdentityNameBeVoted() + "@" + vote.getAbilityName(), vote);
        ec.close();
        return map;
    }

    @Override
    public void saveVote(Vote vote) {
        Transaction txn = getStore().getEnvironment().beginTransaction(null, null);
        vote.setId();
        getPi().put(txn, vote);
        txn.commit();

    }

    @Override
    public void deleteVote(String voteId) {
        Transaction txn = getStore().getEnvironment().beginTransaction(null, null);
        getPi().delete(txn, voteId);
        txn.commit();
    }

    private EntityStore getStore() {
        return voteStore;
    }

    private PrimaryIndex<String, Vote> getPi() {
        return voteIdIndex;
    }

    private SecondaryIndex<String, String, Vote> getIdentityNameBeVotedSi() {
        return votedNameIndex;
    }

    private SecondaryIndex<String, String, Vote> getIdentityNameVoteSi() {
        return voteNameIndex;
    }

    private SecondaryIndex<String, String, Vote> getOwnIdentityNameSi() {
        return ownIdentityNameIndex;
    }

    public static void main(String args[]) {
        String ownerName = "袁岳峰";
        VoteService vs = new VoteServiceImpl();
        Vote vote = new Vote();
        vote.setAbilityName("抗压力");
        vote.setIdentityNameBeVoted("jjz");
        vote.setIdentityNameVote("zq");
        vote.setOwnIdentityName(ownerName);
        vote.setValue(9);
        vs.saveVote(vote);
        vote = new Vote();
        vote.setAbilityName("主动性");
        vote.setIdentityNameBeVoted("o2");
        vote.setIdentityNameVote("jjz");
        vote.setOwnIdentityName(ownerName);
        vote.setValue(9);
        vs.saveVote(vote);
        vote = new Vote();
        vote.setAbilityName("责任感");
        vote.setIdentityNameBeVoted("jjz");
        vote.setIdentityNameVote("yyf");
        vote.setOwnIdentityName(ownerName);
        vote.setValue(9);
        vs.saveVote(vote);
        // List<Vote> list = vs.listVoteMadeBy("o2");
        List<Vote> list = vs.listVoted(ownerName, "o2");
        for (Vote v : list) {
            System.out.println("投票id: " + v.getId());
            System.out.println("能力名: " + v.getAbilityName());
            System.out.println("被投者: " + v.getIdentityNameBeVoted());
            System.out.println("投票者: " + v.getIdentityNameVote());
            System.out.println("得分: " + v.getValue());
        }
    }

}
